import { login } from '@/api/user.js'
import {
  setStorage,
  getStorage,
  removeStorage,
  setTokenTime
} from '@/utils/storage.js'

const state = {
  token: getStorage('token')
}
const mutations = {
  initToken(state, payload) {
    state.token = payload
    setStorage('token', payload)
  },
  logout(state) {
    state.token = null
    removeStorage('token')
  }
}
const actions = {
  async login(context, payload) {
    const res = await login(payload)
    // console.log(res)
    // 存储登录的时间戳
    setTokenTime()
    context.commit('initToken', res.token)
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
